Everything in Python is an 'object'.
Defining custom types of objects is easy:
class Employee:
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def fire(self):
self.salary = 0
Employee.empCount -= 1
empCount
is a class variable, and can be accessed inside and outside of the class as Employee.empCount
Functions within the class (type) definition are called 'methods'.
The first method __init__
is a special method (called the class 'constructor' or initialization method.
You define class methods like normal functions, except that the first argument to each method is the explicit self
parameter.
# This creates the first employee Joe
joe = Employee("Joe",100000)
print('Employee name is ',joe.name)
print(joe.name, 'salary is $', joe.salary)
# This will create the second employee Marc
marc = Employee("Marc",120000)
print('Employee name is ',marc.name)
print(marc.name, 'salary is $', marc.salary)
print("Total employee number = ",Employee.empCount)
We can add, remove and modify attributes at any time:
joe.age = 28
joe.salary = 110000
print('Employee name is ',joe.name)
print(joe.name, 'salary is $', joe.salary)
print(joe.name, 'age is', joe.age)
Let's fire Joe.
joe.fire()
joe.salary
print("Total employee number = ",Employee.empCount)
What is the output of the following code snippet?
a) Error message, because the function Change can't be called in the __init__
function
b) 'Old'
c) 'New'